angular.module('starter.pay', [])
.controller('bankCardListCtrl',function($scope,$faModal,$faAPI,$stateParams,$state,$ionicPopup, $rootScope){
    //银行卡绑定-1.我的银行卡-2.保证金-3.佣金支付
    var gotoType = $stateParams.goid;
    var payM = $stateParams.payM;
    $scope.isSleAgreement = true;
    //console.log(gotoType);
    $scope.sleBankList = function(){
        $scope.modal.banklist();
    };
    var bankName,bankCode;
    var reqData =  $scope.reqData = {};
    $rootScope.isHide = false;

    $faModal('banklist', 'views/pay/bankList.html', $scope, {
        initialize: function() {
            var self = this;
            //获取银行卡列表
            $faAPI.pay.getBankList().then(function(res){
                $scope.bankData = res.list;
            });
            $scope.selectBank = function(data){
                console.log(data.bankShortName);
                $scope.data = data;
                bankName = data.bankName;
                bankCode = data.bankCode;
                self.hide();
            }
        },
        done: function() {
            var self = this;
            self.hide();
        }

    });

    $scope.bindingBank = function(){
        if(!bankName || !bankCode || !$scope.reqData.username || !$scope.reqData.idcardno || !$scope.reqData.phone){
            $ionicPopup.alert({
                title:'请填写完整信息'
            });
            return
        }
        if(!$scope.isSleAgreement){
            $ionicPopup.alert({
                title:'请选择同意委托代扣授权书'
            });
            return
        }
        $scope.reqData.bankname = $scope.data.bankName;
        $scope.reqData.bankcode  = $scope.data.bankCode;
        //去除空格
        $scope.reqData.cardno = $scope.reqData.cardno.replace(/(\d{4})(?=\d)/g,"$1 ").replace(/\s/g,'');

        //绑卡
        $faAPI.pay.bindBank($scope.reqData).then(function(res){
            if(res){
                if(res.errorMsg){
                    $ionicPopup.alert({
                        title: res.errorMsg
                    });
                }else{
                    //我的银行卡-user
                    if(gotoType == 1){
                        $ionicPopup.alert({
                            title:'绑卡成功'
                        });
                        $state.go('myBankList', {goid: 1});
                    }
                    //保证金-支付
                    if(gotoType == 2){
                        $state.go('testMoblie',{payM:payM,cid:res.cardId});
                    }
                    //佣金支付-
                    if(gotoType == 3){
                        $state.go('app.order.inOrder.detail', {
                            id: $rootScope.inOrderDetailsId || 0, pid: $rootScope.inOrderDetailsPid || 0, receiptId: $rootScope.inOrderDetailsReceiptId || 0
                        });
                    }
                    // 提现
                    if(gotoType == 4){
                        $state.go('app.withdraw');
                    }
                }
            }
        });
    };
    //委托代扣协议书
    $scope.SleAgreement = function(){
        $scope.isSleAgreement = !$scope.isSleAgreement;
    };
    $faModal('agreement','views/modals/agreement.html',$scope,{});
    $scope.openAgreement = function(){
        $scope.modal.agreement();
    };
    //银行卡号输入每四位加空格
    $scope.cardFormat = function(){
        var cardFormat =  $scope.reqData.cardno;
        cardFormat = cardFormat.replace(/\s/g,'').replace(/(\d{4})(?=\d)/g,"$1 ");
        $scope.reqData.cardno = cardFormat;
    }

})
